package com.xuanyuan.material

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.xuanyuan.material.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var mBinding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        // bottomNavigation.selectedItemId = R.id.item1 选择具体的Item
        mBinding.bottomNV.setOnItemSelectedListener { item ->
            /**
             * 返回：true 将项目显示为选定项目，如果不应该选择项目，则返回 false。
             * 考虑将不可选择的项目预先设置为禁用，以使它们看起来非交互。
             */
            when (item.itemId) {
                R.id.option_normal_1 -> {
                    // 显示未读数量
                    val badge = mBinding.bottomNV.getOrCreateBadge(item.itemId)
                    badge.isVisible = true
                    // An icon only badge will be displayed unless a number is set:
                    badge.number = 99
                    true
                }
                R.id.option_normal_2 -> {
                    //已读之后隐藏提示
                    val badgeDrawable = mBinding.bottomNV.getBadge(R.id.option_normal_1)
                    badgeDrawable?.isVisible = false
                    badgeDrawable?.clearNumber()
                    true
                }
                R.id.option_normal_3 -> {
                    //删除不再需要的提示
                    val menuItemId = mBinding.bottomNV.menu.getItem(0).itemId
                    mBinding.bottomNV.removeBadge(menuItemId)
                    true
                }
                R.id.option_normal_4 -> {

                    //BadgeDrawable.TOP_END, //BadgeDrawable.TOP_START, //BadgeDrawable.BOTTOM_END, //BadgeDrawable.BOTTOM_START
                    val menuItemId = mBinding.bottomNV.menu.getItem(0).itemId
                    val badgeDrawable = mBinding.bottomNV.getBadge(menuItemId)
                    badgeDrawable?.badgeGravity =BadgeDrawable.TOP_END
                    false
                }
                else -> false
            }
        }


        val badge = mBinding.bottomNV.getOrCreateBadge(R.id.option_normal_1) // Show badge
//        mBinding.bottomNV.removeBadge(R.id.option_normal_1) // Remove badge
//        val badge = mBinding.bottomNV.getBadge(R.id.option_normal_1) // Get badge

        badge.number = 11
        initNavigationView()


        //NavigationView 与 navigation配合使用
        val navView: BottomNavigationView = findViewById(R.id.bottomNV)
        val navController = findNavController(R.id.nav_host_fragment)
        navView.setupWithNavController(navController)

    }

    private fun initNavigationView() {
        // 监听DrawerLayout 动作
        mBinding.drawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener {
            override fun onDrawerSlide(view: View, v: Float) {
                Log.i("---", "滑动中")
            }

            override fun onDrawerOpened(view: View) {
                Log.i("---", "打开")
            }

            override fun onDrawerClosed(view: View) {
                Log.i("---", "关闭")
            }

            override fun onDrawerStateChanged(i: Int) {
                Log.i("---", "状态改变")
            }
        })

        //获取头布局文件,并设置监听
        val headerView = mBinding.navigationView.getHeaderView(0)
        headerView?.setOnClickListener {
        }

        // 监听Item的点击事件
        mBinding.navigationView.setNavigationItemSelectedListener { menuItem ->
            when (menuItem.itemId) {
                R.id.option_normal_1 -> Toast.makeText(this, "第一个", Toast.LENGTH_SHORT).show()
                R.id.option_normal_2 -> {

                }
            }
            true
        }

        // 监听Item的点击事件
        mBinding.navigationView.setNavigationItemSelectedListener { menuItem ->
            when (menuItem.itemId) {
                R.id.option_normal_1 -> Toast.makeText(this, "第一个", Toast.LENGTH_SHORT).show()
            }
            true
        }
    }


}






